<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
?>

<?= yii\web\View::registerCssFile("/css/magic-check.css",['depends' => 'app\assets\AppAsset']) ?>

<div class="modal-header">
	<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	<h4 class="modal-title">选择拼房订单</h4>
</div>

<div style="font-size: 13px; border-radius:1px;">
	<input type="hidden" id="order_id" value=<?= $model->id ?> >
	<input type="hidden" id="room_id" value=<?= $room_id ?> >
	<table class="table table-hover">
	<tr>
		 <th>联系人</th> <th>订单信息</th> <th>备注</th> <th></th>
	</tr>
		<?php foreach($data as $key=>$val){ ?>
		<tr>

			<td><?= $val->username ?> <br>
				<?= $val->phone ?></td>
			<td><?= $val->code ?><br>
				<span style="color:#FF4500;"><?= Yii::$app->formatter->asCurrency($val->price) ?></span>
			</td>
			<td><?= $val->remark ?></td>
			<td class="col-md-5">
				<?php foreach($val->room as $room){ ?>

				<input class="magic-checkbox merge-magic" type="checkbox" id="ass_<?= $room->id?>" name="<?= $room->id?>" data-id=<?= $val->id ?> data-room=<?= $room->id ?>  <?= ($room->merge_id == $room_id) ? 'checked' : '' ?> >
				<label for="ass_<?= $room->id?>" >
					<?php if($room->stock == 2){ ?>
						<i class="fa fa-remove"></i>	
					<?php } ?>
					<?php if($val->product->stock == 1) {?> 

						<?= $room->productRoom->room->name ?> 
						<?= $room->productRoom->room->code ?>
						<?= $room->productRoom->people ?>人间
					<?php }elseif($val->product->stock == 2){ // 海外航线
							$t = json_decode($val->product->rooms, true);
							echo isset($t[$room->room_id]['name']) ? $t[$room->room_id]['name'] : '异常';
							echo "  - ".$room->num . "人";
						}
					?>
					<?php 
							$customers = $room->orderRoomCustomer;
							if($customers){
								foreach($customers as $c){
									//if($c->orderCustomer->status == 1){
									echo ' / '.$c->orderCustomer->first_name . $c->orderCustomer->last_name ; 
									//}	
								}	
							}
					?>
				</label>
				<?php } ?>
			</td>
		</tr>
		<?php } ?>
	</table>
</div>

<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">

    /**  add Loading  **/
    $(".merge-magic").click(function(){
        var order_id	= $("#order_id").val(); // 当前订单编号 
        var room		= $("#room_id").val();  // 当前需要拼房的房间ID

        var merge_id	= $(this).data('id');   // 待拼订单编号
        var room_id		= $(this).data('room');   // 待拼订单对应房型的编号

        var checked 	= $(this).is(':checked');   // 是否选中
		var t = $(this);
		//console.log(order_id);
		//console.log(merge_id);
		//console.log(room_id);
		//return;        
        $.get("/order/order/merge-save",{order_id:order_id, room:room, merge_id:merge_id, room_id:room_id, checked:checked},function(data){
			if(data == 'already_order'){
				alert("该订单已做过拼房, 每个订单只能做一次拼房");
				t.prop("checked", false);
			}else if(data == 'already_room'){
				alert("该房间已做过拼房, 每个房间只能做一次拼房");
				t.prop("checked", false);
			}else if(data == 'room_num'){
				alert("该房间数量不足，无法取消拼房,请在该房间库存数加1间");
				t.prop("checked", true);
			}
        });
    });

	// 清除模态框缓存
	$(".customer-modal").on("hidden.bs.modal", function() {
		$(this).removeData("bs.modal");
		window.location.reload();
	});
</script>
<?php app\components\JsBlock::end() ?>
